﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Blob_Watcher
{
    class BlobLogEntry
    {
        private List<string> properties = new List<string>(); 

        public BlobLogEntry(string logentry)
        {
            int endDelim = 0;
            int currentPos = 0;
            while(currentPos <= logentry.Length-1)
            {

                // if a quoted string... 
                if (logentry.Substring(currentPos,1).StartsWith("\""))
                {
                    currentPos++; // skip opening quote
                    endDelim = logentry.IndexOf("\";", currentPos);
                    if (endDelim == -1) // if no delim, jump to end of string
                        endDelim = logentry.Length - 1;
                    properties.Add(logentry.Substring(currentPos, endDelim - currentPos));
                    // skip ending quote and semicolon
                    endDelim = endDelim + 2;
                }
                else // not quoted string
                {
                    endDelim = logentry.IndexOf(';', currentPos);
                    if (endDelim == -1) // if no delim, jump to end of string
                        endDelim = logentry.Length - 1;
                    properties.Add(logentry.Substring(currentPos, endDelim - currentPos));
                    endDelim++;
                }

                currentPos = endDelim; // advance position
            }

        }

        public string Version { get { return properties[0]; } }
        public string OperationType { get { return properties[2]; } }
        public string RequestStatus { get { return properties[3]; } }
        public string RequestUrl { get { return properties[11]; } }
        public string RequestedObjectKey { get { return properties[12]; } }
        public string eTagIdentifier { get { return properties[24]; } }
        public DateTime LastModified { get { return Convert.ToDateTime(properties[25]); } }

    }
}
